$(document).ready(function () {
    var userstring = sessionStorage.getItem('user');
    var user = JSON.parse(userstring);
    var nickname=user.nickname;
    $('#nickname').text(nickname);
    $.get("pc/trolley/find", function (rs) {
        var fos = rs.result;
        var s = JSON.stringify(fos);
        sessionStorage.setItem("loc",s);
        var printii = 0.0;
        x(fos,printii);

        var item = sessionStorage.getItem("loc");
        items = JSON.parse(item);
        $(document).on("click", "[id='m']", function () {
            var cartCount = $(this).next().val();
            $(this).next().val(++cartCount);
            var val = $(this).next().val();
            let id = $(this).attr("name");
            $.get("pc/trolley/update", {"quantity": val, "fid": id}, function (rs) {
                $.get("pc/trolley/find", function (rs) {
                    var fos = rs.result;
                    $("#f").empty();
                    var printii = 0.0;
                    x(fos,printii);
                });

            });
        });

        $(document).on("click", "[id='n']", function () {
            var cartCount = $(this).prev().val();
            $(this).prev().val(--cartCount);
            var val1 = $(this).prev().val();
            let id = $(this).attr("name");
            $.get("pc/trolley/update", {"quantity": val1, "fid": id}, function (rs) {
                $.get("pc/trolley/find", function (rs) {
                    var fos = rs.result;
                    $("#f").empty();
                    console.log(fos);
                    var printii = 0.0;
                    x(fos,printii);
                });

            });

        });
    });
    $(document).on("click", "a[title='删除']", function () {
        var id = $(this).attr("name");
        $.get("pc/trolley/remove", {"id": id}, function (rs) {
            var mess = rs.result;
            alert(mess);
            $("#f").empty();
            $.get("pc/trolley/find", function (rs) {
                var fos = rs.result;
                console.log(fos)
                var printii = 0.0;
                x(fos,printii)
            });
        });


    })

    $("[name='bb']").click(function () {
        $("#f").empty();
        var ee = $("[name='ss']").val();
        $.get("pc/trolley/find", {"fname": ee}, function (rs) {
            var fos = rs.result;
            console.log(fos)
            var printii = 0.0;
            x(fos,printii)
        });
    });
    $(document).on("click","#check_all",function () {
        $("input[type='checkbox']").not($("#check_all")).each(function () {
            $(this).prop("checked",$("#check_all").prop("checked"));

        });
        zongji();
        $("#zongshu").html($("input[type='checkbox']:checked").not($("#check_all")).length);
    });

    $(document).on("click", "input[type='checkbox']:not(#check_all)", function() {
        if($(this).prop("checked") == false){
            $("#check_all").prop("checked",false);
        }
        else if($("input[type='checkbox']:checked").not($("#check_all")).length == $("input[type='checkbox']").not($("#check_all")).length){
            $("#check_all").prop("checked",true);
        }
        zongji();
        $("#zongshu").html($("input[type='checkbox']:checked").not($("#check_all")).length);

    });

    $(document).on("click","#jiesuan",function () {
        if($("input[type='checkbox']:checked").not($("#check_all")).length <= 0){
            return false;
        }
        else{
            $(this).attr("href",cart());
        }

    });

});


// 1、获取我们所需的标签对象
// 获取表示全选的input标签

function x(fos,printii) {

    var count = 0;
    var list=[];
    for (var i = 0; i < fos.length; i++) {
        $("<ul class=\"cart_list_th clearfix\"><ul class=\"cart_list_td clearfix\">\n" +
            "    <li class=\"col01\"><input type=\"checkbox\"  name='other' checked=''></li>\n" +
            "    <li class=\"col02\"><img src=" + fos[i].image + "></li>\n" +
            "    <li class=\"col03\">" + fos[i].fname + "<br/><em>" + fos[i].price + "/500g</em></li>\n" +
            "    <li class=\"col04\">500g</li>\n" +
            "    <li class=\"col05\">" + fos[i].price + "</li>\n" +
            "    <li class=\"col06\">\n" +
            "        <div class=\"num_add\">\n" +
            "            <a href=\"javascript:;\" class=\"add fl\" id='m' name='"+fos[i].fid+"'>+</a>\n" +
            "            <input type=\"text\" class=\"num_show fl\" value=" + fos[i].quantity + ">\n" +
            "            <a href=\"javascript:;\" class=\"minus fl\" id='n' name='"+fos[i].fid+"'>-</a>\n" +
            "        </div>\n" +
            "    </li>\n" +
            "    <li class=\"col07\">" + fos[i].amount + "</li>\n" +
            "    <li class=\"col08\"><a href=\"javascript:;\" title='删除' name='"+fos[i].id+"'>删除</a></li>\n" +
            "</ul>").appendTo("#f");
        printii = printii + fos[i].amount
        printii = Math.round(printii*100)/100;
        count++
        list[i]=fos[i]

    }
    $("<div class=\"total_count\">全部商品<em>" + count + "</em>件</div>" +
        "<ul class=\"cart_list_th clearfix\">" +
        " <li class=\"col01\">商品名称</li>" +
        "<li class=\"col02\">商品单位</li>" +
        "<li class=\"col03\">商品价格</li>" +
        "<li class=\"col04\">数量</li>" +
        "<li class=\"col05\">小计</li>" +
        "<li class=\"col06\">操作</li>" +
        "</ul>").prependTo("#f")
    $("<ul class=\"settlements\">\n" +
        "    <li class=\"col01\"><input type=\"checkbox\" class='cacaca' name='aaa' id=\"check_all\" checked=\"\"></li>\n" +
        "    <li class=\"col02\">全选</li>\n" +
        "    <li class=\"col03\">合计(不含运费)：<span>¥</span><em id=\"zong\">" + printii + "</em><br>共计<b id=\"zongshu\">" + count + "</b>件商品</li>\n" +
        "    <li class=\"col04\"><a href=\"place_order.html\" id=\"jiesuan\">去结算</a></li>\n" +
        "</ul>").appendTo("#f")
    sessionStorage.setItem("list",list)
}
/*function f(printii) {
    $("<ul class=\"settlements\">\n" +
        "    <li class=\"col01\"><input type=\"checkbox\" name=\"aaa\" checked=\"\"></li>\n" +
        "    <li class=\"col02\">全选</li>\n" +
        "    <li class=\"col03\">合计(不含运费)：<span>¥</span><em>" + printii + "</em><br>共计<b>" + count + "</b>件商品</li>\n" +
        "    <li class=\"col04\"><a href=\"place_order.html\" id='pop'>去结算</a></li>\n" +
        "</ul>").appendTo("#f")
}*/


function xiaoji() {
    $(".col07").html(function () {
        num1 = parseFloat($(this).prevAll().filter(".col05").html());
        num2 = parseInt($(this).prevAll().filter(".col06").find(".num_show").val());
        return ((num1*100)*(num2*100)/10000).toFixed(2)+"元";
    })
}

function zongji(){
    var zong = 0;
    $("input[type='checkbox']").not($("#check_all")).each(function () {
        if($(this).prop("checked")){
            zong += parseFloat($(this).parent().nextAll().filter(".col07").html());
        }
    });
    $("#zong").html(zong.toFixed(2));

    if($("input[type='checkbox']:checked").not($("#check_all")).length <= 0)
    {
        $("#jiesuan").css("background-color",'#fff4e8');
    }
    else{
        $("#jiesuan").css("background-color",'#ff3d3d');
    }
}



/*function cart() {
    var url="/order/orderinfo?";
    $("input[type='checkbox']:checked").not($("#check_all")).each(function (i) {
        url += "cart" + i + "=" + $(this).parents(".cart_list_td").attr("id") +"&";
    });
    return url;
}*/
